<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE sqlMap PUBLIC
    "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
    "http://ibatis.apache.org/dtd/sql-map-2.dtd">
    
<sqlMap namespace="${beanName}">
	<resultMap class="${modelPackage}.${beanName}" id="${beanName}">
		<#list properties as p>
		<result property="${p.propertyName}" column="${p.columnName}" />
		</#list>
	</resultMap>
  
	<sql id="masterStatement">
		<![CDATA[SELECT * FROM ${tableName} ]]>
	</sql>

	<sql id="findWhereStatement">
		<#list properties as p>
		<#if p.propertyTypeName == "java.lang.String">
		<isNotEmpty prepend="AND" property="${p.propertyName}"> ${p.columnName}=#${p.propertyName}# </isNotEmpty>
		<#else>
		<isNotNull prepend="AND" property="${p.propertyName}"> ${p.columnName}=#${p.propertyName}# </isNotNull>
		</#if>
		</#list>
	</sql>
  
	<select id="getCount${beanName}" parameterClass="${modelPackage}.${beanName}" resultClass="java.lang.Integer">
		<![CDATA[
	    	SELECT COUNT(*) CNT FROM ${tableName} 
		]]>
		<dynamic prepend="WHERE">
			<include refid="findWhereStatement" />
		</dynamic>
	</select>
  
	<select id="find${beanName}" parameterClass="${modelPackage}.${beanName}" resultMap="${beanName}">
		<include refid="masterStatement" />
		<dynamic prepend="WHERE">
			<include refid="findWhereStatement" />
		</dynamic>
	</select>

	<select id="find${beanName}ByPage" parameterClass="java.util.HashMap" resultMap="${beanName}">
		<![CDATA[
			SELECT * FROM (
			SELECT A.*,ROWNUM RN FROM (
		]]>
		<include refid="masterStatement" />
		<dynamic prepend="WHERE">
			<include refid="findWhereStatement" />
		</dynamic>
		<![CDATA[
			) A WHERE ROWNUM < #endRow#
			) WHERE RN >= #startRow#
	    ]]>
	</select>

	<select id="get${beanName}ById" parameterClass="java.lang.Long" resultMap="${beanName}">
		<include refid="masterStatement" />
		<![CDATA[
			WHERE ID=#value#
		]]>
	</select>

	<insert id="insert${beanName}" parameterClass="${modelPackage}.${beanName}">
		<selectKey resultClass="java.lang.Long" keyProperty="id" type="pre">
		<![CDATA[
			SELECT SEQ_ID.NEXTVAL AS ID FROM DUAL 
		]]>
		</selectKey>
		<![CDATA[
			INSERT INTO ${tableName} 
		]]>
		<dynamic prepend="(" >
			<#list properties as p>
			<#if p.propertyTypeName == "java.lang.String">
			<isNotEmpty prepend="," property="${p.propertyName}" > ${p.columnName} </isNotEmpty>
			<#else>
			<isNotNull prepend="," property="${p.propertyName}" > ${p.columnName} </isNotNull>
			</#if>
			</#list>
			)
		</dynamic>
	    VALUES 
		<dynamic prepend="(" >
			<#list properties as p>
			<#if p.propertyTypeName == "java.lang.String">
			<isNotEmpty prepend="," property="${p.propertyName}" > #${p.propertyName}# </isNotEmpty>
			<#else>
			<isNotNull prepend="," property="${p.propertyName}" > #${p.propertyName}# </isNotNull>
			</#if>
			</#list>
			)
		</dynamic>
	</insert>
  
	<update id="update${beanName}ById" parameterClass="${modelPackage}.${beanName}">
		<![CDATA[
			UPDATE ${tableName}
		]]>
		<dynamic prepend="SET">
			<#list properties as p>
			<isNotNull prepend="," property="${p.propertyName}"> ${p.columnName}=#${p.propertyName}# </isNotNull>
			</#list>
		</dynamic>
		<![CDATA[
			WHERE ID=#id#
		]]>
	</update>

	<delete id="delete${beanName}ById" parameterClass="java.lang.Long">
		DELETE ${tableName}
		<![CDATA[
			WHERE ID=#value#
		]]>
	</delete>
	
	<delete id="delete${beanName}ByIds" parameterClass="map">
		DELETE ${tableName}
		<iterate prepend="where" property="ids" open="(" close=")" conjunction="OR">
			ID =#ids[]#
		</iterate>
	</delete>
    
</sqlMap>